package cn.initcap.nio.tcp;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;

/**
 * 客户端线程类，专门接收服务器端响应信息
 *
 * @author initcap
 * @date Created in 4/29/19 10:19 PM.
 */
public class ClientHandler implements Runnable {

    private Selector selector;

    public ClientHandler(Selector selector) {
        this.selector = selector;
    }

    @Override
    public void run() {
        try {
            int j = 0;
            while (true) {
                j++;
                if (j == Integer.MAX_VALUE) {
                    break;
                }
                // 获取可用的channel数量
                int readChannels = selector.select();

                if (readChannels == 0) {
                    continue;
                }

                // 获取可用channel的集合
                Set<SelectionKey> selectionKeyList = selector.selectedKeys();

                Iterator iterator = selectionKeyList.iterator();
                while (iterator.hasNext()) {
                    SelectionKey selectionKey = (SelectionKey) iterator.next();

                    // 移除Set中的当前selectionKey
                    iterator.remove();

                    //7. 根据就绪状态，调用对应方法处理业务逻辑

                    // 如果是 可读事件
                    if (selectionKey.isReadable()) {
                        readHandler(selectionKey, selector);
                    }

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理可读事件
     */
    private void readHandler(SelectionKey selectionKey, Selector selector) throws IOException {
        // 要从selectionKey中获取到已经就绪的channel
        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();

        // 创建buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        // 循环读取服务器端响应信息
        String response = "";
        while (socketChannel.read(byteBuffer) > 0) {
            // 切换为读模式
            byteBuffer.flip();

            // 读取buffer中内容
            response += Charset.forName("utf-8").decode(byteBuffer);
        }

        // 将channel再次注册到selector上，监听他的可读事件
        socketChannel.register(selector, SelectionKey.OP_READ);

        // 将服务端响应信息打印到本地
        if (response.length() > 0) {
            // 广播给其他客户端
            System.out.println(response);
        }

    }

}
